<?php
require 'Model.php';
class UserModel extends Model{

    function getUserList(){
        $sql="select * from users";

        $data=$this->conn->query($sql);
        return $data;
    }
    function delUser($uid){
        $sql="delete from users where uid=$uid";
        $result=$this->conn->query($sql);
        return $result;
    }

    function regUser($username,$password,$intro,$pic){
        if($_FILES["img"]["error"]>0)
        {
            switch ($_FILES["img"]["error"]) {
                case 1:echo "文件上传过大";
                    break;
                case 3:echo "文件上传失败";
                    break;
                case 4:echo "没有文件上传";

                    $sql="insert into users values(null,'$username','$password','$intro',null)";

                    break;
                default:echo "未知错误"; exit;

            }
        }
        else
        {  //上传文件}
            echo $_FILES["img"]["name"];
            $uploadpic=$_FILES["img"]["name"];
            $filepath="./photos/";
            $newname=$this->upload($uploadpic,$filepath);

            $sql="insert into users values(null,'$username','$password','$intro','$newname')";
        }
        $result=$this->conn->query($sql);
        return $result;
    }
    function getUser($uid){
        $sql="select * from users where uid=$uid";
        $data=$this->conn->query($sql);
        return $data;
    }
    function editUser($id,$username,$password,$intro,$pic){


        if($_FILES["img"]["error"]>0)
        {
            switch ($_FILES["img"]["error"]) {
                case 1:echo "文件上传过大";
                    break;
                case 3:echo "文件上传失败";
                    break;
                case 4:echo "没有文件上传";

                    $sql="update users set uname='{$username}',password='{$password}',intro='{$intro}where uid={$id}";

                    break;
                default:echo "未知错误"; exit;

            }
        }
        else {  //上传文件}
            echo $_FILES["img"]["name"];
            $uploadpic=$_FILES["img"]["name"];
            $filepath="./photos/";
            $newname=$this->upload($uploadpic,$filepath);

            $sql = "update users set uname='{$username}',password='{$password}',intro='{$intro}',pic='{$newname}'where uid={$id}";

        }
        $result=$this->conn->query($sql);
        return $result;
    }

}
?>